<template>
<div>
    <div v-for="(item,index) in goodslist" :key="index" >
        <tr>
            <td>书名:{{item.title}}</td>
        </tr>
        <tr>
            <td>价格:{{item.price}}</td>
        </tr>
        <tr>
            <td>库存:{{item.num}}</td>
        </tr>
        <tr>

            <Button type="submit" @click="buy(item.title,item.price,item.num)">立即购买</Button>
        </tr>
        <table>
            <tr>选择优惠券</tr>
            <tr v-for="(item,index) in coun" :key="index">
                <td>{{item.coupon}}</td>
            </tr>
        </table>

    </div>
</div>
</template>

<script>
export default {
    data () {
        return {
            goodslist:[],
            coun:[],
            cartlist:[]
            
       
 
        }
    },
    methods: {
         showuser(){
             this.axios.get('http://127.0.0.1:8000/myupup/myupdegoods/',{params:{'username':this.username,'password':this.password}})
             .then(result=>{
                 this.goodslist=result.data.message
                 console.log(result)
            
             })      
       },
    
        buy(title,price){
            if(localStorage.getItem('username')==null){
                this.$router.push('/myuplogin')
                return false
            }
            this.cartlist.push({'title':title,'price':price})         
            console.log('打印单个',this.cartlist)
            //里面只能存放字符串，所以需要强转字符串
            localStorage.setItem('cart',JSON.stringify(this.cartlist))
             this.$router.push('learnorder')
      
            this.axios.post('http://127.0.0.1:8000/myupup/myupdecart/',{'title':title,'price':price})
             .then(result=>{
       
                 console.log(result)
            
             })      
           
        },
         showcoun(){
             this.axios.get('http://127.0.0.1:8000/myupup/myupdecoupon/')
             .then(result=>{
                 this.coun=result.data.message
                 console.log(result)
            
             })      
       },
    

       
        
    },
    mounted() {
        this.showuser()
        this.showcoun()
    },

}
</script>

<style>

</style>
